package com.wpw.web.work.rifit;

/**
 * @Description:
 * @ClassName: ConsoleRefitJsCreater
 * @CreateDate: 2020-06-15 16:43
 * @author: wupengwei
 */

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
 * 步骤：
 * 1.先从库存中导出库存数据：留下sku,sn两列
 * 2.将所有的sku编码复制出来，查询skuId,（product_code	id） 结果复制到Excel中，
 * 3.将skuId复制到pids.txt中，查询裸机skuid，之后将对应的sku关系放到excel中；（改前id	裸机id）
 * 4，查询逻辑sku的skuCode  i裸机d	product_code
 *
 *
 * 商品编码	SN	裸机sku编码			product_code	id	裸机skuid	裸机sku编码						改前id	裸机id				i裸机d	product_code
 * @Description:
 * 文本以======分隔
 * 改前sku======sn======改后sku
 * @ClassName: RifitJsCreater
 * @CreateDate: 2020-03-28 12:04
 * @author: wupengwei
 */
public class ConsoleRefitJsCreater {

    public static void main(String[] args) {
        String filePath = "/Users/wupengwei/Desktop/改装脚本/改装sn.txt";
        Path path = Paths.get(filePath);
        try {
            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
            int totalSize = lines.size();
            System.out.println("共" + totalSize + "条数据");

            Map<String, List<String>> skuT2SNMap = new HashMap<>();
            Map<String, String> skuT2SkuMap = new HashMap<>();

            for (int i = 0; i < lines.size(); i++) {

                String line = lines.get(i);
                String[] split = line.split("======");
                String sSku = split[0];
                String sn = split[1];
                String tSku = split[2];

                List<String> snList = skuT2SNMap.get(sSku);
                if (snList == null) {
                    snList = new ArrayList<>();
                    skuT2SNMap.put(sSku, snList);
                }
                snList.add(sn);

                skuT2SkuMap.put(sSku, tSku);

            }


            StringBuilder head = new StringBuilder("$('#refit_pisAdd_outwarehouse').select2('val',['296']);")
                    .append("$('#refit_pisAdd_inwarehouse').select2('val',['296']);")
                    .append("$('#refit_pisAdd_department').select2('val',['10']);")
                    .append("$('#refit_pisAdd_remark').val('协助整修翻账，将历史设备改为裸机');");

            StringBuilder tail = new StringBuilder("var e = jQuery.Event('keyup');")
                    .append("e.keyCode = 13;")
                    .append("$('#refit_pisAddInTable_productCode_1').trigger(e);");



            /*$('#refit_pisAdd_outwarehouse').select2('val',['242']);
            $('#refit_pisAdd_inwarehouse').select2('val',['242']);
            $('#refit_pisAdd_department').select2('val',['10']);
            $('#refit_pisAdd_remark').val('协助整修翻账，将历史设备改为裸机');


            $('#scanAddTable_productCode_1').val('R9ENVF7');
            refitOrderScanAdd.productCodeChange(document.getElementById('scanAddTable_productCode_1'));
            $('#scanAddTrToTable').click();

            $('#scanAddTable_productCode_2').val('R9FYPD8');
            refitOrderScanAdd.productCodeChange(document.getElementById('scanAddTable_productCode_2'));
            $('#scanAddTrToTable').click();

            $('#scanOut_submit').click();
            $('#refit_pisAddInTable_productCode_1').val('ELIBX22004WF1NBNP');


            var e = jQuery.Event('keyup');
            e.keyCode = 13;
            $('#refit_pisAddInTable_productCode_1').trigger(e);*/


            //构造中间部分
            int size = skuT2SNMap.size();
            System.out.println("一共" + size + "条");

            Set<String> sSkuSet = skuT2SNMap.keySet();
            Map<String, StringBuilder> snSetSbMap = new LinkedHashMap<>();
            for (String sSku : sSkuSet) {
                List<String> snList = skuT2SNMap.get(sSku);
                StringBuilder snSetSb = new StringBuilder("");
                for (int i = 0; i < snList.size(); i++) {
                    int index = i + 1;
                    String sn = snList.get(i);
                    snSetSb.append("$('#scanAddTable_productCode_"+index+"').val('"+sn+"');")
                            .append("refitOrderScanAdd.productCodeChange(document.getElementById('scanAddTable_productCode_"+index+"'));")
                            .append("$('#scanAddTrToTable').click();");

                }
                snSetSbMap.put(sSku, snSetSb);
            }



            //写js
            String jsFile = "/Users/wupengwei/Desktop/改装脚本/改装分批sn.js";
            Path jsPath = Paths.get(jsFile);
            BufferedWriter writer = Files.newBufferedWriter(jsPath, StandardCharsets.UTF_8);
            for (String sSku : sSkuSet) {
                String tSKu = skuT2SkuMap.get(sSku);
                writer.newLine();
                writer.write("/******************************/");
                writer.newLine();
                writer.write(head.toString());
                writer.write(snSetSbMap.get(sSku).toString());

                writer.write("$('#scanOut_submit').click();");
                writer.write("$('#refit_pisAddInTable_productCode_1').val('"+tSKu+"');");
                writer.write("$('#refit_pisAddInTable_num_1').val($('#refit_pisAddOutTable_num_1').val());");
                writer.write(tail.toString());

            }

            writer.flush();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}